package com.xopencode.comparable;

/**
 * @author Mcb
 * @date 2022/6/19
 * @apiNote
 */
public class Product implements Comparable {
    private String name;
    private double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Product() {
    }

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Product) {
            Product p = (Product) o;
            if (this.getName().equals(p.getName())) {
                return Double.compare(this.getPrice(),p.getPrice());
            } else {
                return this.getName().compareTo(p.getName());
            }
        }
        return 0;
    }
}
